package com.sora.sora.security.login;

import com.sora.sora.common.constant.ResultCode;
import com.sora.sora.common.model.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Slf4j
public class MyEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        log.error(e.getMessage());
        httpServletResponse.setContentType("application/json; charset=UTF-8");
        PrintWriter writer = httpServletResponse.getWriter();
        ResultVO<String> resultVO = new ResultVO<>(ResultCode.UNAUTHORIAZED, "请登录");
        writer.write(resultVO.toString());
        writer.flush();
        writer.close();
    }
}
